﻿using System;
using System.Data;
using System.Data.OleDb;
//comm -- / <summary>
//comm -- / Get the native type based on the database type
//comm -- / </summary>
//comm -- / <param name="dbType">The database type to convert</param>
//comm -- / <returns>The equivalent managed type, otherwise the DBNull type</returns>
namespace nsDb
{
    public class DbTypeConverter
        {
       
        public static Type ConvertTypeForApp ( DbType dbType )
            {
            Type toReturn = typeof ( DBNull );

            switch (dbType)
                {
                case DbType.String:
                    toReturn = typeof ( string );
                    break;

                case DbType.UInt64:
                    toReturn = typeof ( UInt64 );
                    break;

                case DbType.Int64:
                    toReturn = typeof ( Int64 );
                    break;

                case DbType.Int32:
                    toReturn = typeof ( Int32 );
                    break;

                case DbType.UInt32:
                    toReturn = typeof ( UInt32 );
                    break;

                case DbType.Single:
                    toReturn = typeof ( float );
                    break;

                case DbType.Date:
                    toReturn = typeof ( DateTime );
                    break;

                case DbType.DateTime:
                    toReturn = typeof ( DateTime );
                    break;

                case DbType.Time:
                    toReturn = typeof ( DateTime );
                    break;

                case DbType.StringFixedLength:
                    toReturn = typeof ( string );
                    break;

                case DbType.UInt16:
                    toReturn = typeof ( UInt16 );
                    break;

                case DbType.Int16:
                    toReturn = typeof ( Int16 );
                    break;

                case DbType.SByte:
                    toReturn = typeof ( byte );
                    break;

                case DbType.Object:
                    toReturn = typeof ( object );
                    break;

                case DbType.AnsiString:
                    toReturn = typeof ( string );
                    break;

                case DbType.AnsiStringFixedLength:
                    toReturn = typeof ( string );
                    break;

                case DbType.VarNumeric:
                    toReturn = typeof ( decimal );
                    break;

                case DbType.Currency:
                    toReturn = typeof ( double );
                    break;

                case DbType.Binary:
                    toReturn = typeof ( byte [ ] );
                    break;

                case DbType.Decimal:
                    toReturn = typeof ( decimal );
                    break;

                case DbType.Double:
                    toReturn = typeof ( Double );
                    break;

                case DbType.Guid:
                    toReturn = typeof ( Guid );
                    break;

                case DbType.Boolean:
                    toReturn = typeof ( bool );
                    break;
                } //eof switch

            return toReturn;
            }

        public static DbType ConvertFromDbTypeStrToDbTypeForDb ( string dbTypeStr )
            {

            DbType toReturn = DbType.Xml;

            if (dbTypeStr.Equals ("varchar"))
            {	toReturn = DbType.String	; 
            }

            else if  (dbTypeStr.Equals("bigint")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"bigint\"))");
	            toReturn = DbType.Int32	;
            }

            else if  (dbTypeStr.Equals("binary")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"binary\"))");
	            toReturn = DbType.Binary	; 
            }

            else if  (dbTypeStr.Equals("bit")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"bit\"))");
	            toReturn = DbType.Boolean	; 
            }

            else if  (dbTypeStr.Equals("char")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"char\"))");
	            toReturn = DbType.String	; 
            }

            else if  (dbTypeStr.Equals("datetime")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"datetime\"))");
	            toReturn = DbType.DateTime	; 
            }

            else if  (dbTypeStr.Equals("decimal")) 
            {
                //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"decimal\"))");
	            toReturn = DbType.Decimal	; 
            }

            else if  (dbTypeStr.Equals("float")) 
            {
                //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"float\"))");
	            toReturn = DbType.Double	; 
            }

            else if  (dbTypeStr.Equals("image")) 
            {
                //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"image\"))");
	            toReturn = DbType.Object	; 
            }

            else if  (dbTypeStr.Equals("int")) 
            {
                //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"int\"))");
	            toReturn = DbType.Int64	; 
            }

            else if  (dbTypeStr.Equals("money")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"money\"))");
	            toReturn = DbType.Decimal	; 
            }

            else if  (dbTypeStr.Equals("nchar")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"nchar\"))");
	            toReturn = DbType.String	; 
            }

            else if  (dbTypeStr.Equals("ntext")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"ntext\"))");
	            toReturn = DbType.String	; 
            }

            else if  (dbTypeStr.Equals("numeric")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"numeric\"))");
	            toReturn = DbType.Double	;
            }

            else if  (dbTypeStr.Equals("nvarchar")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"nvarchar\"))");
	            toReturn = DbType.String	; 
            }

            else if  (dbTypeStr.Equals("real")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"real\"))");
	            toReturn = DbType.Single	; 
            }

            else if  (dbTypeStr.Equals("smalldatetime")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"smalldatetime\"))");
	            toReturn = DbType.Date	; 
            }

            else if (dbTypeStr.Equals("smallint"))
	            {
                
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"smallint\"))");
	            toReturn = DbType.Int16;
            }

            else if  (dbTypeStr.Equals("smallmoney")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"smallmoney\"))");
	            toReturn = DbType.Decimal	; 
            }

            else if  (dbTypeStr.Equals("sql_variant")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"sql_variant\"))");
	            toReturn = DbType.String	; 
            }

            else if  (dbTypeStr.Equals("sysname")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"sysname\"))");
	            toReturn = DbType.String	; 
            }

            else if  (dbTypeStr.Equals("text")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"text\"))");
	            toReturn = DbType.String	; 
            }

            else if  (dbTypeStr.Equals("timestamp")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"timestamp\"))");
	            toReturn = DbType.DateTime	; 
            }

            else if  (dbTypeStr.Equals("tinyint")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"tinyint\"))");
	            toReturn = DbType.Byte	; 
            }

            else if  (dbTypeStr.Equals("uniqueidentifier")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"uniqueidentifier\"))");
	            toReturn = DbType.Guid	; 
            }

            else if  (dbTypeStr.Equals("varbinary")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"varbinary\"))");
	            toReturn = DbType.Binary	; 
            }

            else if  (dbTypeStr.Equals("varchar")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"varchar\"))");
	            toReturn = DbType.String	; 
            }

            else if  (dbTypeStr.Equals("xml")) 
            {
            //debugOk Utils.Debugger.WriteIf ("case is (dbTypeStr.Equals(\"xml\"))");
	            toReturn = DbType.Xml	; 
            }

else {	;}
            return toReturn;
           } //eof ConvertTypeForDb (string dbType ) 

        public static string TranslatoToCSharpType(string type)
        {
          string result = string.Empty;

          switch (type)
          {
            case "char":
            case "varchar":
            case "nchar":
            case "nvarchar":
            case "text":
            case "ntext":
              {
                result = "string";
                break;
              }
            case "bit":
              {
                result = "bool";
                break;
              }
            case "int":
              {
                result = "int";
                break;
              }
            case "tinyint":
              {
                result = "byte";
                break;
              }
            case "bigint":
              {
                result = "long";
                break;
              }
            case "smallint":
              {
                result = "short";
                break;
              }

            case "datetime":
              {
                result = "DateTime";
                break;
              }
            case "float":
              {
                result = "float";
                break;
              }
            case "real":
              {
                result = "double";
                break;
              }
            case "numeric":
              {
                result = "float";
                break;
              }
            default:
              {
                result = "string";
                break;
              }
          }
          return result;

        } //eof method 
        } //eof class DbTypeConverter

    } //eof namespace nsDb